//
//  MWelcomeVCViewController.swift
//  iOSweibo
//
//  Created by lzm on 16/5/14.
//  Copyright © 2016年 lzm. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class MWelcomeVCViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        prepareUI()
        setIcon()
        //获取用户信息
        MUserAccountViewModel.sharedInstance.loadUserInfo { () -> () in
            self.setIcon()
        }
    }
    //获取用户数据,设置头像
    private func setIcon()
    {
        if let avatar_large = MUserAccountViewModel.sharedInstance.userAccount?.avatar_large{
            
            self.iconView.sd_setImageWithURL(NSURL(string: avatar_large), placeholderImage: UIImage(named: "avatar_default_big"))
        }
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        makeAnimation()
    }
    
    private func makeAnimation(){
        
        UIView.animateWithDuration(1, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            
            
            //更新frame
            self.iconView.snp_updateConstraints(closure: { (make) -> Void in
                make.bottom.equalTo(self.view).offset(-(UIScreen.mainScreen().bounds.height - 160))
            })
            
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(1, animations: { () -> Void in
                    self.messageLabel.alpha = 1.0
                    
                    }, completion: { (_) -> Void in
//                        MPrint(items: "动画完成~")
                        //切换控制器
                        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
                        appDelegate.switchRootVC(MtabBarController())
                        
                })
                
        }
        
    }
    
    private func prepareUI(){
        //添加到父控件
        view.addSubview(bkgView)
        view.addSubview(iconView)
        view.addSubview(messageLabel)
        
        //背景图添加约束
        bkgView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self.view)
        }
        //用户头像添加约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.view)
            make.bottom.equalTo(self.view).offset(-160)
        }
        //添加信息
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.view)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
        
        
    }
    
    private lazy var iconView : UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    
    private lazy var bkgView : UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    private lazy var messageLabel : UILabel = {
            let messageLabel = UILabel()
        
        messageLabel.text = "欢迎归来"
        messageLabel.textColor = UIColor.blackColor()
        messageLabel.font = UIFont.systemFontOfSize(15)
        messageLabel.sizeToFit()
        messageLabel.alpha = 0
    
            return messageLabel
        }()
    
//    override func loadView() {
//        
//    }

}
